package com.wcj.linktable.classicquestion;

import com.wcj.linktable.ListNode;

import java.util.Stack;

/**
 * 给你一个单链表的头节点 head ，请你判断该链表是否为回文链表。如果是，返回 true ；否则，返回 false 。
 * 输入：head = [1,2,2,1]
 * 输出：true
 */
public class 回文链表 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(1);
        boolean palindrome = isPalindrome(head);
        System.out.println(palindrome);
    }

    /**
     * 思路：将链表的后半部分反转，然后前后半比较
     * @param head
     * @return
     */
    public static boolean isPalindrome(ListNode head) {
        int len = 0;
        ListNode tmp = head;
        int frontTailIdx = 0;
        int endHeadIdx = 0;
        ListNode testLen = head;
        while (testLen.next != null){
            len++;
            testLen = testLen.next;
        }
        len++;
        frontTailIdx = len/2 - 1;
        if (len % 2 == 0){
            endHeadIdx = frontTailIdx + 1;
        }else {
            endHeadIdx = frontTailIdx + 2;
        }
        tmp = head;
        for (int i = 0; i < endHeadIdx - 1; i++){
            tmp = tmp.next;
        }
        //反转
        ListNode pre = tmp;
        ListNode cur = tmp.next;
        for (int i = 0; i < len - endHeadIdx;i++){
            pre.next = cur.next;
            cur.next = null;
            testLen.next = cur;
            cur = pre.next;
        }
        ListNode front = head;
        ListNode end = tmp.next;
        boolean result = true;
        while (end != null){
            if (front.val != end.val){
                result = false;
            }
            front = front.next;
            end = end.next;
        }

        return result;
    }

    public boolean isPalindrome2(ListNode head) {
        if (head == null) {
            return true;
        }

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode firstHalfEnd = endOfFirstHalf(head);
        ListNode secondHalfStart = reverseList(firstHalfEnd.next);

        // 判断是否回文
        ListNode p1 = head;
        ListNode p2 = secondHalfStart;
        boolean result = true;
        while (result && p2 != null) {
            if (p1.val != p2.val) {
                result = false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }

        // 还原链表并返回结果
        firstHalfEnd.next = reverseList(secondHalfStart);
        return result;
    }

    private ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    private ListNode endOfFirstHalf(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
